package com.zhou.demo.zhihn;


import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zhou.demo.adapter.DateRecycleViewAdapter;
import com.zhou.demo.callback.LatestNewsCallback;
import com.zhou.demo.callback.NewsInformationCallback;
import com.zhou.demo.interfaceclass.LastestNewsConnect;
import com.zhou.demo.interfaceclass.NewsInformationConnect;
import com.zhou.demo.model.DateDetail;
import com.zhou.demo.model.MyDate;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class ZhiHuNewLatestActivity extends AppCompatActivity implements
        SwipeRefreshLayout.OnRefreshListener, LatestNewsCallback,NewsInformationCallback {

    @ViewInject(R.id.recycleView)
    private RecyclerView recyclerView;
    @ViewInject(R.id.refreshLayout)
    private SwipeRefreshLayout refreshLayout;
    private LastestNewsConnect lastestNewsConnect;
    private List<MyDate> list = new ArrayList<>();
    private DateRecycleViewAdapter dateRecycleViewAdapter;
    private RecyclerView.LayoutManager layoutManager;
    private NewsInformationConnect newsInformationConnect;
    private int lastVisiblePosition;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_zhi_hu_new_latest);
        x.view().inject(this);
        refreshLayout.setColorSchemeResources(android.R.color.holo_orange_light, android.R.color.holo_blue_bright,
                android.R.color.holo_green_light, android.R.color.holo_red_light);
        refreshLayout.setOnRefreshListener(this);
        dateRecycleViewAdapter = new DateRecycleViewAdapter(this, list);
        layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setAdapter(dateRecycleViewAdapter);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addOnScrollListener(onScrollListener);
        lastestNewsConnect = new LastestNewsConnect();
        newsInformationConnect = new NewsInformationConnect();
    }

    RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
            lastVisiblePosition = layoutManager.findLastVisibleItemPosition();

        }

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);

        }
    };

    private void onRecycleViewNewState(int newState){
        if(newState== RecyclerView.SCROLL_STATE_IDLE && lastVisiblePosition + 1 == recyclerView.getLayoutManager().getItemCount()){
            if(list.get(lastVisiblePosition).getDate().equals("20130519")){
                Snackbar.make(recyclerView,"已经到最后一页了",Snackbar.LENGTH_SHORT).show();
            }else{
                preLoad();
            }
        }
    }

    private void preLoad(){

    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshLayout.post(new Runnable() {

            @Override
            public void run() {
                refreshLayout.setRefreshing(true);
            }
        });
        onRefresh();
    }

    @Override
    public void onRefresh() {
        lastestNewsConnect.getLastestNews(this);
    }

    @Override
    public void onLatestNewsSuccess(String succeed) {
        Log.e("latestNews", succeed);
        list.clear();
        try {
            JSONObject jsonObject = new JSONObject(succeed);
            String date = jsonObject.optString("date");
            MyDate myDate = new MyDate();
            myDate.setDate(date);
            Gson gson = new Gson();
            Type type = new TypeToken<List<DateDetail>>() {
            }.getType();
            List<DateDetail> dateDetails = gson.fromJson(jsonObject.getJSONArray("stories").toString(), type);
            myDate.setList(dateDetails);
            list.add(myDate);
            dateRecycleViewAdapter.update(list);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        refreshLayout.setRefreshing(false);

    }

    @Override
    public void onLatestNewsFail(String failed) {

    }

    @Override
    public void onNewsInformationSuccess(String newsInformation) {

    }

    @Override
    public void onNewsInformationFail(String newsFailedInformation) {

    }
}
